package com.artron.mediaartron.ui.linkpage;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.LayoutRes;
import android.util.Property;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.artron.baselib.image.ImageUtils;
import com.artron.baselib.utils.UIUtils;
import com.artron.mediaartron.R;
import com.artron.mediaartron.base.BaseLinkPage;

import butterknife.BindView;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-11-22.
 */

public class EditLinkPage extends BaseLinkPage {

    @BindView(R.id.EditLinkPage_iv_top)
    ImageView mIvTop;
    @BindView(R.id.EditLinkPage_iv_center)
    ImageView mIvCenter;
    @BindView(R.id.EditLinkPage_ib_know)
    Button mIbKnow;

    private int mCurrentPage;
    private int[] mTopRes;
    private int[] mAnimRes;
    private ObjectAnimator mAnimator;

    private EditLinkPage(ViewGroup parent, @LayoutRes int layoutId) {
        super(parent, layoutId);
    }

    public EditLinkPage(ViewGroup parent) {
        this(parent, R.layout.layout_edit_link_page);
    }

    @Override
    protected void initVariable() {
        mTopRes = new int[]{R.drawable.ic_link_page_edit_transition_prompt,
                R.drawable.ic_link_page_edit_scale_prompt,
                R.drawable.ic_link_page_edit_rotate_prompt};

        mAnimRes = new int[]{R.drawable.ic_link_page_edit_transition_anim,
                R.drawable.animation_edit_scale,
                R.drawable.animation_edit_rotate};
    }

    @Override
    protected void initView() {
        mAnimator = initCenterAnimator(mIvCenter, View.TRANSLATION_X, -100, 100);

        switchPage();
        mIbKnow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchPage();
            }
        });
    }

    private ObjectAnimator initCenterAnimator(View target, Property<View, Float> property,
                                              float... values) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(target, property, values);
        animator.setRepeatCount(1000);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.setDuration(3000);
        return animator;
    }

    private void switchPage() {
        switch (mCurrentPage) {
            case 0:
                mIvTop.setImageResource(mTopRes[mCurrentPage]);
                mIvCenter.setImageResource(mAnimRes[mCurrentPage]);
                mAnimator.start();
                break;
            case 1:
            case 2:
                mIvCenter.setTranslationX(0);
                mAnimator.cancel();
                Drawable ivDrawable = mIvCenter.getDrawable();
                if (ivDrawable != null && ivDrawable instanceof AnimationDrawable) {
                    ((AnimationDrawable) ivDrawable).stop();
                }

                mIvTop.setImageResource(mTopRes[mCurrentPage]);
                Drawable drawable = UIUtils.getDrawable(mAnimRes[mCurrentPage]);
                mIvCenter.setImageDrawable(drawable);
                if (drawable instanceof AnimationDrawable) {
                    if (!((AnimationDrawable) drawable).isRunning()) {
                        ((AnimationDrawable) drawable).start();
                    }
                }
                break;
            default:
                mContainer.removeView(mContentView);
                break;
        }
        mCurrentPage++;
    }
}
